package com.rzj.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rzj.book.bo.EmpBO;
import com.rzj.book.dao.IEmpDao;
import com.rzj.book.dao.IMenuDao;
import com.rzj.book.dao.IRoleDao;
import com.rzj.book.en.Emp;
import com.rzj.book.en.Menu;
import com.rzj.book.en.Role;
import com.rzj.book.en.Tree;
import com.rzj.book.service.IEmpService;
import com.rzj.book.utils.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName EmpServiceImpl
 * @Description TODO
 * @Author Administrator
 * @Date 2020/5/17 13:51
 * @Version 1.0
 */
@Service
@Transactional
public class EmpServiceImpl implements IEmpService {

    @Autowired
    private IEmpDao empDao;

    @Autowired
    private IMenuDao menuDao;

    @Autowired
    private IRoleDao roleDao;

    @Override
    public Emp findByUsernameAndPwd(String username, String pwd) {
        return empDao.findByUsernameAndPwd(username, pwd);
    }

    @Override
    public Menu readMenusByEmpuuid(Integer empId) {
        //获取所有的菜单，做模板用的
        Menu root = menuDao.getALLMenu(0);
        //用户下的菜单集合
        List<Menu> empMenus = menuDao.findMenuListByEmpId(empId);
        List<Long> empList = new ArrayList<>();
        for (int i = 0; i < empMenus.size(); i++) {
            empList.add(empMenus.get(i).getMenuid());
        }

        System.out.println(empMenus.size());
        //根菜单
        Menu menu = cloneMenu(root);

        //循环匹配模板
        //一级菜单
        Menu _m1 = null;
        Menu _m2 = null;
        for (Menu m1 : root.getMenus()) {
            _m1 = cloneMenu(m1);
            //二级菜单循环
            for (Menu m2 : m1.getMenus()) {
                //用户包含有这个菜单
                if (empList.contains(m2.getMenuid())) {
                    //复制菜单
                    _m2 = cloneMenu(m2);
                    //加入到上级菜单下
                    _m1.getMenus().add(_m2);
                }
            }
            //有二级菜单我们才加进来
            if (_m1.getMenus().size() > 0) {
                //把一级菜单加入到根菜单下
                menu.getMenus().add(_m1);
            }
        }
        return menu;
    }

    @Override
    public List<Emp> getEmpList() {
        return empDao.getEmpList();
    }

    @Override
    public List<Tree> readEmpRoles(Integer id) {
        List<Tree> treelist = new ArrayList<Tree>();
        Emp emp = empDao.getEmpByEmpId(id);
        List<Role> empRoles = emp.getRoleList();
        List<Role> roleList = roleDao.getRoleList();

        List<Long> roleIdList = new ArrayList<>();
        if(!empRoles.isEmpty()){
           for(int i=0;i<empRoles.size();i++){
               roleIdList.add(empRoles.get(i).getRoleId());
           }
        }

        Tree t1 = null;
        for (Role role : roleList) {
            t1 = new Tree();
            t1.setId(role.getRoleId());
            t1.setText(role.getName());
            if (roleIdList.contains(role.getRoleId())) {
                t1.setChecked(true);
            }
            treelist.add(t1);
        }
        return treelist;
    }

    @Override
    public void insert(Emp emp) {
        empDao.insert(emp);
    }

    @Override
    public PageResult<Emp> getEmpBOList(Integer page, Integer rows, EmpBO empBO) {
        PageHelper.startPage(page, rows);
        List<Emp> list = getListByExample(empBO);
        PageInfo<Emp> info = new PageInfo<Emp>(list);
        return new PageResult<Emp>(info.getTotal(), info.getList());
    }

    @Override
    public Emp getEmpById(Integer id) {
        return empDao.getEmpByEmpId(id);
    }

    @Override
    public void deleteEmpById(Integer id) {
        empDao.deleteEmpById(id);
    }

    public List<Emp> getListByExample(EmpBO empBO){
        Example example = new Example(Emp.class);
        Example.Criteria criteria = example.createCriteria();
        if(empBO!= null){
            if(!StringUtils.isEmpty(empBO.getName())){
                criteria.andLike("name","%"+empBO.getName()+"%");
            }

            if(!StringUtils.isEmpty(empBO.getUsername())){
                criteria.andLike("username","%"+empBO.getUsername()+"%");
            }

            if(!StringUtils.isEmpty(empBO.getAddress())){
                criteria.andLike("address","%"+empBO.getAddress()+"%");
            }

            if(empBO.getGender() != null){
                criteria.andEqualTo("gender",empBO.getGender());
            }

            if(!StringUtils.isEmpty(empBO.getEmail())){
                criteria.andLike("email","%"+empBO.getEmail()+"%");
            }

            if(!StringUtils.isEmpty(empBO.getTele())){
                criteria.andLike("tele","%"+empBO.getTele()+"%");
            }

            if(!StringUtils.isEmpty(empBO.getStartDate()) && !StringUtils.isEmpty(empBO.getEndDate())){
                criteria.andBetween("birthday",empBO.getStartDate(),empBO.getEndDate());
            }

            if(empBO.getDepId() != null){
                criteria.andEqualTo("depId",empBO.getDepId());
            }
        }
        List<Emp> emps = empDao.selectByExample(example);
        return emps;
    }

    private Menu cloneMenu(Menu src) {
        Menu menu = new Menu();
        menu.setIcon(src.getIcon());
        menu.setMenuid(src.getMenuid());
        menu.setMenuName(src.getMenuName());
        menu.setUrl(src.getUrl());
        menu.setMenus(new ArrayList<Menu>());
        return menu;
    }

}
